package com.example.springbott.service;

import com.example.springbott.bean.MarketKeyword;
import com.example.springbott.bean.MarketKeywordExample;
import com.example.springbott.bean.common.AdminKeywordListVO;
import com.example.springbott.bean.common.KeywordCreateBO;
import com.example.springbott.bean.common.KeywordListBO;
import com.example.springbott.mapper.MarketKeywordMapper;
import com.example.springbott.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * @author 1784549954@qq.com
 * @since 2022/11/20 20:41
 */
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public AdminKeywordListVO list(KeywordListBO keywordListBO) {
        PageHelper.startPage(keywordListBO.getPage(),keywordListBO.getLimit());
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.setOrderByClause(keywordListBO.getSort()+" "+keywordListBO.getOrder());
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(keywordListBO.getKeyword())){
            criteria.andKeywordLike("%"+keywordListBO.getKeyword()+"%");
        }
        if(!StringUtil.isEmpty(keywordListBO.getUrl())){
            criteria.andUrlLike("%"+keywordListBO.getUrl()+"%");
        }

        AdminKeywordListVO<MarketKeyword> listVO = new AdminKeywordListVO<MarketKeyword>();
        List<MarketKeyword> list = marketKeywordMapper.selectByExample(marketKeywordExample);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(list);
        listVO.setPage(pageInfo.getPageNum());
        listVO.setTotal((int) pageInfo.getTotal());
        listVO.setList(list);
        listVO.setPages(pageInfo.getPages());
        listVO.setLimit(pageInfo.getPageSize());
        return listVO;
    }

    @Override
    @Transactional
    public void keywordCreate(MarketKeyword marketKeyword) {
        Date date = new Date();
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);
        marketKeywordMapper.insertSelective(marketKeyword);
    }

    @Override
    @Transactional
    public void keywordUpdate(MarketKeyword marketKeyword) {
        Date date = new Date();
        marketKeyword.setUpdateTime(date);
        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
    }

    @Override
    @Transactional
    public void keywordDelete(MarketKeyword marketKeyword) {
        marketKeywordMapper.updateByHeheerOfDelete(marketKeyword.getId());
    }
}
